package com.neu.springboottest2;



import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.query.SQLQuery;
import org.junit.platform.commons.util.StringUtils;

import java.util.Collections;
import java.util.Scanner;


public class CodeGenerator2 {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    private static final String projectPath = System.getProperty("user.dir");
    private static final String url = "jdbc:mysql://localhost:3306/test0701?useSSL=false&serverTimezone=UTC";//这里改为自己数据库名字
    private static final String username = "root";//数据库用户名和密码
    private static final String password = "ROOT";
    private static final String parentPackageName = "com.neu.springboottest2";//生成的mapper等文件夹放在哪个包下
    private static final String writer = "phil";//自己的名字
    private static final String outPath = projectPath + "\\src\\main\\java\\";
    private static final String mapperPath = projectPath + "\\src\\main\\resources\\com\\neu\\springboottest2\\mapper\\";//mapper.xml文件放的位置

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder(url, username, password)
            .databaseQueryClass(SQLQuery.class) // 设置SQL查询方式，默认的是元数据查询方式
            .build();


    public static void main(String[] args) {
        String[] tableNames = {"emp","dept","salgrade","bonus","userinfoa"};//数据库表名
        CodeGenerator2.execute(tableNames);
    }

    public static void execute(String[] tableNames) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(writer)
//                            .enableSwagger()
                            .outputDir(outPath)
                            .disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder.parent(parentPackageName)
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mapperPath));
                })

                .strategyConfig(builder -> {
                    builder.addInclude(tableNames)
//                            .addTablePrefix("t_")
                            //Service 层生成设置
                            .serviceBuilder()
                            .formatServiceFileName("I%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            //Entity
                            .entityBuilder()
                            .entityBuilder().fileOverride()//表示再次生成会覆盖，建议entity和mapper层覆盖
                            .enableChainModel()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .logicDeleteColumnName("deleted")
                            .logicDeletePropertyName("deleted")
                            .versionColumnName("version")
                            .versionPropertyName("version")
                            .addTableFills(new Column("create_Time", FieldFill.INSERT))
                            .addTableFills(new Column("update_Time", FieldFill.INSERT_UPDATE))
                            //controller
                            .controllerBuilder()
                            .enableRestStyle()//生成的controller自带@RestController注解
                            //mapper
                            .mapperBuilder()
                            .mapperBuilder().fileOverride();
                })
                .templateEngine(new VelocityTemplateEngine()) //使用freemaker模板引擎
                .execute();
    }
}

